const express = require('express')
const router =express.Router()
const OpinionModel = require('../models/opinion')

router.get('/find',(req,res,next)=>{
    
  OpinionModel.find().then(result=>{
    res.json({
        code:200,
        data:result,
        message:'全局查询成功',
    })
})
})

router.post('/create',(req,res,next)=>{
  const {content,user_id} = req.body
  OpinionModel.find().then(result=>{
      const opinion = new OpinionModel({
        opinion_id:result.length?result[result.length-1].opinion_id+1:0,
        content,
        user_id,
        create_time:new Date(new Date().valueOf()+8*60*60*1000),
      })
      opinion.save().then(result => {
          res.json({
              code:200,
              data:result,
              message:'Opinion创建完成',
          })
      })
  })
})

router.delete('/delete',(req,res,next)=>{
  const {opinion_id} = req.body

  OpinionModel.deleteOne({opinion_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'删除完成',
      })
  })
})

module.exports = router